package org.jeecg.modules.demo.pointcount.redis.thread;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.moenydata.entity.Moneydatahappycafe;
import org.jeecg.modules.demo.moenydata.service.IMoneydatahappycafeService;
import org.jeecg.modules.demo.moneylevel.service.IMoneyLevelService;
import org.jeecg.modules.demo.pointcount.entity.*;
import org.jeecg.modules.demo.pointcount.redis.entity.AsyncTaskData;
import org.jeecg.modules.demo.pointcount.redis.thread.work.*;
import org.jeecg.modules.demo.pointcount.redis.util.Config;
import org.jeecg.modules.demo.pointcount.service.ICheckDataService;
import org.jeecg.modules.demo.pointcount.service.IEventDataService;
import org.jeecg.modules.demo.pointcount.service.IOnlineDataService;
import org.jeecg.modules.demo.pointcount.service.ISpecialEventService;
import org.jeecg.modules.demo.userexist.entity.UserExistCountData;
import org.jeecg.modules.demo.userexist.service.IUserExistCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author yw
 * @Date 2021/3/16 9:21
 */
@Slf4j
@Component
public class AsyncTaskManager {
    //private static Logger logger = Logger.getLogger(AsyncTaskManage.class);

    //private ExecutorService cachedThreadPool = Executors.newCachedThreadPool();//短信获取处理线程池
    private static int sqlThreadPoolNum = 10;// sql专用
    private ExecutorService sqlFixThreadPool = Executors.newFixedThreadPool(sqlThreadPoolNum);//sql专用

    @Autowired
    IEventDataService eventDataService;

    @Autowired
    ICheckDataService checkDataService;

    @Autowired
    ISpecialEventService specialEventService;

    @Autowired
    IOnlineDataService onlineDataService;

    @Autowired
    IMoneydatahappycafeService moneydatahappycafeService;

    @Autowired
    IUserExistCountService iUserExistCountService;


    private static final AsyncTaskManager instance = new AsyncTaskManager();

    public static final AsyncTaskManager getInstance() {
        return instance;
    }

    /**
     * 添加对象获取任务
     *
     * @param data
     */
    public void addTask(AsyncTaskData<?> data) {
        try {
//            if (cachedThreadPool.isShutdown()) {
//                log.error("AsyncTaskManager异常关闭！");
//                cachedThreadPool = Executors.newCachedThreadPool();
//            }

            if (sqlFixThreadPool.isShutdown()) {
                log.error("AsyncTaskManager异常关闭,sqlFixThreadPool！");
                sqlFixThreadPool = Executors.newFixedThreadPool(sqlThreadPoolNum);
            }

            parseTask(data);
        } catch (Exception e) {
            log.error(e.getMessage());

            //异常时重启线程池
//            if (cachedThreadPool != null) {
//                cachedThreadPool.shutdown();
//            }
//            cachedThreadPool = Executors.newCachedThreadPool();

            //异常时重启线程池 sql部分
            if (sqlFixThreadPool != null) {
                sqlFixThreadPool.shutdown();
            }
            sqlFixThreadPool = Executors.newFixedThreadPool(sqlThreadPoolNum);

            parseTask(data);
        }
    }



    private void parseTask(AsyncTaskData<?> data) {
        Runnable task = null;
        if(data.getCmd().equals(Config.TASK_ADD_CHECKDATA)){// checkdata事件任务
            CheckData params = (CheckData) data.getData();
            task = new AddCheckDataTask(checkDataService,params);
        }else if(data.getCmd().equals(Config.TASK_ADD_ONLINEDATAADD)){// onlinedata新增事件任务
            OnlineData params = (OnlineData) data.getData();
            task = new AddOnlineDataTask(onlineDataService,params);
        }else if(data.getCmd().equals(Config.TASK_ADD_ONLINEDATAUPDATE)){// onlinedata更新事件任务
            OnlineData params = (OnlineData) data.getData();
            task = new UpdateOnlineDataTask(onlineDataService,params);
        }else if(data.getCmd().equals(Config.TASK_ADD_SPECIALEVENTDATA)){// specialeventdata事件任务
            SpecialEventData params = (SpecialEventData) data.getData();
            task = new AddSpecialEventDataTask(specialEventService,params);
        }else if(data.getCmd().equals(Config.TASK_ADD_EVENTDATA)){// eventdata事件任务
            EventData params = (EventData) data.getData();
            task = new AddEventDataTask(eventDataService,params);
        }else if(data.getCmd().equals(Config.TASK_ADD_MONEYDATA)){// moneydata事件任务
            Moneydatahappycafe params = (Moneydatahappycafe) data.getData();
            task = new AddMoneyDataTask(moneydatahappycafeService,params);
        }else if(data.getCmd().equals(Config.TASK_ADDONLINEDATACOUNT)){// 在线玩家统计事件任务
            OnlineData params = (OnlineData) data.getData();
            task = new AddOnlineDataCountTask(onlineDataService,params);
        }else if(data.getCmd().equals(Config.TASK_ADD_USEREXISTCOUNTDATA)){// 用户留存统计事件任务
            UserExistCountData params = (UserExistCountData) data.getData();
            task = new AddUserExistCountDataTask(iUserExistCountService,params);
        }
        if (task != null) {
            //if (data.getCmd().equals(Config.TASK_SQL_PROCESS)) {
                sqlFixThreadPool.execute(task);
//            }
//            else {
//                cachedThreadPool.execute(task);
//            }
        }
    }
}
